/*
 * $Id: AbstractActionWrapper.java 23 2009-03-31 23:37:36Z aaron.day $
 */
package org.crazydays.gameplan.swing;


import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * AbstractActionWrapper
 */
public abstract class AbstractActionWrapper
        extends AbstractAction
{
	/** log */
	protected static Log LOG =
	        LogFactory.getLog(AbstractActionWrapper.class);

	/**
	 * Load icon from resource within the classpath.
	 * 
	 * @param resource Resource
	 * @return Icon
	 */
	protected static Icon loadIcon(String resource)
	{
		if (resource == null) {
			return null;
		}

		InputStream inputStream =
		        AbstractActionWrapper.class
		                .getResourceAsStream(resource);
		if (inputStream == null) {
			LOG.warn("Unable to load icon: " + resource);
			return null;
		}

		try {
			return new ImageIcon(ImageIO.read(inputStream));
		} catch (IOException e) {
			LOG.warn("Unable to load icon: " + resource, e);
			return null;
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				LOG.warn("Error closing inputStream", e);
			}
		}
	}

	/**
	 * AbstractActionWrapper constructor.
	 */
	public AbstractActionWrapper()
	{
		super();
	}

	/**
	 * AbstractActionWrapper constructor.
	 * 
	 * @param text Text
	 */
	public AbstractActionWrapper(String text)
	{
		super(text);
	}

	/**
	 * AbstractActionWrapper constructor.
	 * 
	 * @param text Text
	 * @param icon Icon
	 */
	public AbstractActionWrapper(String text, Icon icon)
	{
		super(text, icon);
	}
}
